package com.learn.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * TTl队列
 */
@Configuration
public class DelayedQueueConfig {

    public static final String DELAYED_EXCHANGES_NAME = "delayed.exchange";
    public static final String DELAYED_QUEUE_NAME = "delayed.queue";
    public static final String DELAYED_ROUTING_KEY_NAME = "delayed.routingkey";


    //声明队列
    @Bean
    public Queue delayedQueue() {
        return new Queue(DELAYED_QUEUE_NAME);
    }

    //延迟交换机
    @Bean
    public CustomExchange delayedExchange() {
        Map<String, Object> arguments=new HashMap<>();
        arguments.put("x-delayed-type", "direct");
        /**
         * String name, 交换机名称
         * String type, 交换机类型
         * boolean durable, 是否需要持久化
         * boolean autoDelete, 是否需要自动删除
         * Map<String, Object> arguments 其他参数
         */
        return new CustomExchange(
                DELAYED_EXCHANGES_NAME,
                "x-delayed-message",
                true,
                false,
                arguments);
    }





    @Bean
    public Binding delayedQueueBindingDelayedExchange(
            @Qualifier("delayedQueue") Queue delayedQueue,
            @Qualifier("delayedExchange") CustomExchange delayedExchange){
        return BindingBuilder
                .bind(delayedQueue)
                .to(delayedExchange)
                .with(DELAYED_ROUTING_KEY_NAME)
                .noargs();
    }


}
